var voiceModuleJs = function(targetObj){
	this.sysUtils = new kendoUtils();
	this.targetObj = targetObj;
	this.init();
	return this;
};

voiceModuleJs.prototype={
		init:function(){
			var bindFieldArray = this.sysUtils.getBindFieldArray(this.targetObj);
			var bindFunctionArray = [{fName:"saveequOrg",fs:this.saveequOrg},{fName:"cancel",fs:parkTree.resetTree}];
			this.bindBaseModels = new bindBaseModel(null,bindFieldArray,bindFunctionArray,null);
			this.sysUtils.bindDoucment(this.bindBaseModels,this.targetObj,this);
			this.initNumberWidget();
			this.loadEquinfo();
		},
		initNumberWidget:function(){
            var that = this;
			var init = function(div,minNumber,maxNumber){
				div.kendoNumericTextBox({
					min:minNumber,
					max:maxNumber,
					format:"#",
                    change:that.changeDeviceNumberByInput,
                    spin:that.changeDeviceNumberBySpin
				});
			};
			/*init(that.targetObj.find("#deviceNumber"),1,99);
			init(that.targetObj.find("#deviceport"),0,65535);*/
		},
		changeDeviceNumberByInput:function () {
			var no = this.value();
			var _this = this;
			var code = voiceModule.editResult.organizationCode == undefined ? '':voiceModule.editResult.organizationCode;
			JsonpAjax(baseApiPath+'facilities/checkCtrlNo?ctrlNo='+no+'&organizationCode='+code,null,"json",function(result){
				if(result.success){
					layer.tips('设备机号'+no+'已存在！！！',_this._text[0],{tipsMore: true,tips: [2, '#78BA32']});
					_this._text[0].value = '';
				}
			});
		},
		changeDeviceNumberBySpin:function () {
			var no = this.value();
			var _this = this;
            var code = voiceModule.editResult.organizationCode == undefined ? '':voiceModule.editResult.organizationCode;
			JsonpAjax(baseApiPath+'facilities/checkCtrlNo?ctrlNo='+no+'&organizationCode='+code,null,"json",function(result){
				if(result.success){
					layer.tips('设备机号'+no+'已存在！！！',_this.element[0],{tipsMore: true,tips: [2, '#78BA32']});
					_this.element[0].value = '';
				}
			});
		},
		loadEquinfo:function (e) {
			var that = this;
			that.editResult = null;
			that.isEdit = null;
			if (!isEmpty(e)) {
				that = $(e.target).data("that");
			}
			var selectedNodes = zTree.getSelectedNodes()[0];
			var node = selectedNodes.getParentNode();  //父节点
			if(node.gpc == "equ"){   //如果父节点为设备，说明走的是板载串口，不用设置机号
				this.targetObj.find("#changeEquIp").remove();
			}
			var id = selectedNodes.id,level=selectedNodes.level;
			var url = baseApiPath+"facilities/parkInfo";
			if(id){
				that.isEdit = true;
				url += '?isEdit='+that.isEdit+'&organizationCode='+id;
			}else{
				that.isEdit = false;
				url = baseApiPath+'facilities/addOrz?isEdit='+that.isEdit+'&pid='+node.id+'&lev='+level+'&gpc=equ';
			}
			//显示相应的设备信息
			Utils.createAjaxDropDownList({div:that.targetObj.find("#deviceType"),select:[{code:parkTree.downDevJson["deviceType"],name:parkTree.downDevJson["deviceName"]}],unshift:false},that);
			that.sysUtils.mvvmModel.set("equipmentManagements_basic_deviceid",parkTree.downDevJson["deviceType"]);
			JsonpAjax(url,null,"json",function(result){
				that.editResult = result;
				that.sysUtils.setVariantBindFieldValue(result);
			},false);

			parkTree.hideMask();
		},
		//保存设备信息
		saveequOrg:function(e){
			var that =  $(e.target).data("that");
			if(validityForm(that.targetObj)){
				var parkInfo = that.sysUtils.getBindDocJson();
				var url = baseApiPath+"facilities/saveParkInfo?isEdit=" + that.isEdit;
				var sendJson=parkInfo;
				for(var o in parkInfo){
					if(o.indexOf('_') !=-1){
						var strArray = o.substring(o.indexOf('_')+1);
						var name = o.substring(0, o.indexOf('_'));
						var jsonA = {};
						jsonA[name] = that.recursionObjectName(strArray,parkInfo[o]) ;
						delete parkInfo[o];
						sendJson =$.extend(true,parkInfo,jsonA);
					}
				}
				JsonpAjax(url,JSON.stringify(sendJson),"json",function(result){
                        that.sysUtils.setVariantBindFieldValue(result);
                    	that.isEdit=true;
                        parkTree.localUpdate(result);
				});
			}
		},
		recursionObjectName : function (strArray,val) {
			if (strArray.indexOf('_') == -1){
				var jsonC = {};
				jsonC[strArray]=val;
				return jsonC;
			}
			var name = strArray.substring(0, strArray.indexOf('_'));
			strArray = strArray.substring(strArray.indexOf('_')+1);
			var jsonB = {};
			jsonB[name] = this.recursionObjectName(strArray,val);
			return jsonB;
		}
}
